export const setSession = (key: string, data: unknown) => {
  if (data === undefined) {
    sessionStorage.removeItem(key)
  } else {
    sessionStorage.setItem(key, JSON.stringify(data))
  }
}

export const getSession = <T>(key: string) => {
  const data = sessionStorage.getItem(key)
  return data && (JSON.parse(data) as T)
}

export const removeSession = (key: string) => {
  return sessionStorage.removeItem(key)
}

export const setLocal = (key: string, data: unknown) => {
  if (data === undefined) {
    localStorage.removeItem(key)
  } else {
    localStorage.setItem(key, JSON.stringify(data))
  }
}

export const getLocal = <T>(key: string) => {
  const data = localStorage.getItem(key)
  return data && (JSON.parse(data) as T)
}

export const removeLocal = (key: string) => {
  return localStorage.removeItem(key)
}
